<template>
  <div>
    <el-container>
      <el-header style="margin-top: 10px;height: auto;">
        <el-row>
          <single_website_select :website="website_id" :multiple="false" size="small" @change="changeWebsite" />
          <el-select v-model="finish" size="small" placeholder="请选择状态">
            <el-option label="所有" value="" />
            <el-option label="操作完成" value="true" />
            <el-option label="等待完成" value="false" />
          </el-select>
          <el-input
            v-model="kwInput"
            placeholder="输入 任意字符 进行筛选"
            size="small"
            class="input-with-select"
            clearable
            @keyup.native.enter="handleSearch"
          >
            <el-button slot="append" size="small" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          </el-input>
          <el-button
            size="small"
            type="primary"
            class="el-icon-plus add-btn"
            :disabled="listLoading"
            @click="handleAction('create', '添加')"
          >添加
          </el-button>
        </el-row>
      </el-header>
      <el-main>
        <el-table
          v-loading.fullscreen.lock="listLoading"
          :data="data"
          border
          fit
          highlight-current-row
          style="width: 100%"
          @cell-mouse-enter="(m) => m.active=true"
          @cell-mouse-leave="(m) => m.active=false"
          @sort-change="handleSort"
        >
          <el-table-column align="center" width="65px" prop="image" label="图片">
            <template slot-scope="scope">
              <img :src="scope.row.imageLink" style="max-width: 50px; max-height: 50px">
            </template>
          </el-table-column>
          <el-table-column align="center" label="domain" prop="domain" sortable="custom" />
          <el-table-column align="center" label="product_id" prop="product_id" sortable="custom">
            <template slot-scope="scope">
              <a :href="scope.row.url" target="_blank">{{ scope.row.product_id }}</a>
            </template>
          </el-table-column>
          <el-table-column align="center" label="替换产品" prop="change_crawl_url" sortable="custom">
            <template slot-scope="scope">
              <img v-if="scope.row.new_image !== ''" :src="scope.row.new_image" style="max-width: 50px; max-height: 50px">
              <a :href="scope.row.change_crawl_url" target="_blank">打开</a>
            </template>
          </el-table-column>
          <el-table-column align="center" label="reason" prop="reason" sortable="custom" />
          <el-table-column align="center" label="finish" prop="finish" sortable="custom">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.finish" type="success">{{ scope.row.finish }}</el-tag>
              <el-tag v-else type="danger">{{ scope.row.finish }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="create_at" prop="create_at" sortable="custom">
            <template slot-scope="scope">
              <p>{{ scope.row.create_at }}</p>
              <p>{{ scope.row.operator_name }}</p>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="450">
            <template slot-scope="scope">
              <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>
              <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>

            </template>
          </el-table-column>

        </el-table>
        <el-pagination
          v-if="data.length"
          background
          class="mt-pagination"
          :current-page="page"
          :page-sizes="[10,20,50,100]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </el-main>
    </el-container>
    <el-dialog
      append-to-body
      :title="title"
      :width="width"
      :visible.sync="innerDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="temp" label-position="left" label-width="120px">
        <el-form-item label="网站">
          <single_website_select :website="temp.website_id" :multiple="false" size="small" @change="changeModifyWebsite" />
        </el-form-item>
        <el-form-item label="产品ID">
          <el-input v-model="temp.product_id" />
        </el-form-item>
        <el-form-item label="reason">
          <el-input v-model="temp.reason" />
        </el-form-item>
        <el-form-item label="替换产品">
          <el-input v-model="temp.change_crawl_url" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/orders/productoptimization'
import qs from 'qs'
import Single_website_select from '../erp/elicink/elements/single_website_select'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'ProductOptimization',
  components: { Single_website_select },
  data() {
    return {
      feedproductVisual: false,
      product_id: '',
      domain: '',
      website_id: '',
      finish: '',
      name: '产品优化',
      data: [],
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,

      action: null,
      dialogVisible: false,
      title: '',
      temp: {
        product_id: '',
        website_id: '',
        change_crawl_url: '',
        reason: ''
      },
      width: '',
      innerDialogVisible: false
    }
  },
  methods: {
    changeModifyWebsite(v) {
      this.temp.website_id = v
    },
    handleModifyFeed(row) {
      if (row.website_id) {
        this.website_id = parseInt(row.website_id)
        this.product_id = row.product_id
        this.domain = row.domain
        this.feedproductVisual = true
      }
    },
    changeWebsite(v) {
      this.website_id = v
    },
    resetTemp() {
      this.temp = {
        product_id: '',
        website_id: '',
        change_crawl_url: '',
        reason: ''
      }
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        pagesize: this.pagesize,
        page: this.page,
        finish: this.finish,
        order_by: this.order_by,
        website_id: this.website_id,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = '添加'
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.url}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改${row.domain}`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          Post(_data
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, _data).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    }
  }
}
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}

.top >>> .el-select .el-input {
  width: 350px;
}

.top >>> .input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.top {
  padding-left: 20px;
  margin-bottom: 30px;
  margin-top: 28px;
}

.input-with-select {
  width: 400px;
}

.add-btn {
  float: right;
  margin-right: 20px;
  width: 150px
}

.mt-pagination {
  float: right;
  margin-top: 5px;
}

.app-container >>> .el-dialog {
  margin-top: 48px !important;
  margin-bottom: 68px;
}

.mt-dialog >>> .el-transfer-panel {
  width: calc(50% - 60px);
}

.mt-form >>> .el-select {
  width: 100%;
}

.mt-form >>> .el-input__inner {
  width: 100%;
}

.mt-dialog >>> .el-dialog {
  margin-top: 60px;
  /*margin: 168px auto !important;*/
}

.mt-dialog >>> .el-dialog__body {
  padding-bottom: 100px;
}
</style>
